/**
 * @project leadnews-project
 * @author MECHREVO
 * @date 2024/1/25 19:54:01
 * @version 1.0
 */
package com.heima.article.service.impl;

import com.heima.article.service.ArticleInfoService;
import com.heima.common.redis.CacheService;
import com.heima.model.article.dtos.ArticleInfoDto;
import com.heima.model.article.pojos.ArticleCollectPojo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.BehaviorEnums;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.AppThreadLocalUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class ArticleInfoServiceImpl implements ArticleInfoService {
    @Autowired
    private CacheService cacheService;
    /**
     * 文章行为数据回显
     *
     * @param articleInfoDto
     * @return
     */
    @Override
    public ResponseResult ArticleDataEcho(ArticleInfoDto articleInfoDto) {
        //从redis中拿到数据
        ApUser user1 = AppThreadLocalUtil.getUser();
        ArticleCollectPojo articleCollectPojo = new ArticleCollectPojo();
        articleCollectPojo.setIsLike(false);
        articleCollectPojo.setIsUnlike(false);
        articleCollectPojo.setIsCollection(false);
        articleCollectPojo.setIsFollow(false);
        if(user1!=null&&user1.getId()!=0) {
            //第一步拿到是否关注的数据
            Set<String> User = cacheService.scan("UserId_*");
            Set<String> Author = cacheService.scan("AuthorId_*");
            //如果关注了就加高亮
            if (User != null && !User.isEmpty() && Author != null && !Author.isEmpty()) {
                articleCollectPojo.setIsFollow(true);
            }
            //是否收藏了收藏了加高亮
            Set<String> Collection = cacheService.scan("COLLECTION_BEHAVIOR_*");
            if (Collection != null && !Collection.isEmpty()) {
                articleCollectPojo.setIsCollection(true);
            }
            //是否点赞了点赞了加高亮
            Set<String> Like = cacheService.scan("LIKE_BEHAVIOR_*");
            if (Like != null && !Like.isEmpty()) {
                articleCollectPojo.setIsLike(true);
            }
            //是否不喜欢了不喜欢了加高亮
            Set<String> UnLike = cacheService.scan("UN_LIKE_*");
            if (UnLike != null && !UnLike.isEmpty()) {
                articleCollectPojo.setIsUnlike(true);
            }
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("isfollow", articleCollectPojo.getIsFollow());
            resultMap.put("islike", articleCollectPojo.getIsLike());
            resultMap.put("isunlike", articleCollectPojo.getIsUnlike());
            resultMap.put("iscollection", articleCollectPojo.getIsCollection());
            return ResponseResult.okResult(resultMap);
        }
        return null;
    }
}
